# 注意事项

  1. Scanner 相比 BufferedReader 可以方便的定义分割符,以及进行各种数据类型的判断,但是如果只是以键盘输入数据,BufferedReader 更合适
  2. 最常见的开发输入输出搭配:BufferedReader/Scanner 和 PrintStream/PrintWriter

# 样例代码

public class KeyboardInputReader {
    private static final BufferedReader INPUT = 
        new BufferedReader(new InputStreamReader(System.in));

    public String getString(String prompt) {
        System.out.println(prompt);
        String value = null;
        try {
            value = INPUT.readLine();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
        return value;
    }

    public String getStringNotNull(String prompt) {
        String value = null;
        while (true) {
            value = this.getString(prompt);
            if (value != null && !"".equals(value)) {
                break;
            } else {
                System.out.println("Input can not be empty, try again.");
            }
        }
        return value;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Last Updated: 7/2/2020, 4:20:51 PM